package com.chaodev.base.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.chaodev.base.dao.MsgDao;
import com.chaodev.base.dto.MsgDto;
import com.chaodev.base.model.Msg;
import com.chaodev.base.service.MsgService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * vcbase
 *
 * @author 95634
 * @描述
 * @date 2023/5/18 10:01
 **/
@Service
public class MsgServiceImpl implements MsgService {
    @Autowired
    MsgDao dao;

    @Override
    public Msg findDataById(String id) {
        return dao.findDataById(id);
    }

    @Override
    @Transactional(rollbackFor=Exception.class)
    public void saveData(Msg tbMsg) throws DataAccessException{
        if(ObjectUtil.isNotNull(tbMsg.getId())){
            dao.updateDataById(tbMsg);
        }else{
            dao.insertData(tbMsg);
        }
    }

    @Override
    @Transactional(rollbackFor=Exception.class)
    public void insertData(Msg tbMsg) throws DataAccessException{
        dao.insertData(tbMsg);
    }

    @Override
    @Transactional(rollbackFor=Exception.class)
    public void updateDataById(Msg tbMsg) throws DataAccessException {
        dao.updateDataById(tbMsg);
    }

    @Override
    @Transactional(rollbackFor=Exception.class)
    public void deleteDataById(String id) throws DataAccessException{
        dao.deleteDataById(id);
    }

    @Override
    public List<Msg> queryDataList(Msg tbMsg) {
        return dao.queryDataList(tbMsg);
    }

    @Override
    public PageInfo<Msg> queryPageList(MsgDto dto) {
        PageHelper.startPage(dto.getPage(), dto.getRows());
        Msg msg = new Msg();
        BeanUtil.copyProperties(dto,msg);
        List<Msg> tbMsgList = dao.queryDataList(msg);
        PageInfo<Msg> pageInfo = new PageInfo<Msg>(tbMsgList, dto.getRows());
        return pageInfo;
    }

    @Override
    public void readMsg(String id) {
        dao.readMsg(id);
    }
}
